{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c6382b9-7e31-4886-93ad-130ce9cdcda9",
   "metadata": {},
   "source": [
    "# langchain langGraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "94884457-8793-46d5-811e-c8754a081e08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "Hi Bob! 👋 How's it going? What can I do for you today? 😊\n"
     ]
    }
   ],
   "source": [
    "#%pip install langgraph\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "from langgraph.graph import START, MessagesState, StateGraph\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import AIMessage,HumanMessage\n",
    "\n",
    "model = ChatOpenAI(\n",
    "  openai_api_key=\"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\",\n",
    "  openai_api_base=\"https://openrouter.ai/api/v1\",\n",
    "  model_name = \"deepseek/deepseek-chat-v3-0324:free\"\n",
    "  )\n",
    "\n",
    "# Define a new graph\n",
    "workflow = StateGraph(state_schema=MessagesState)\n",
    "\n",
    "\n",
    "# Define the function that calls the model\n",
    "def call_model(state: MessagesState):\n",
    "    response = model.invoke(state[\"messages\"])\n",
    "    return {\"messages\": response}\n",
    "\n",
    "\n",
    "# Define the (single) node in the graph\n",
    "workflow.add_edge(START, \"model\")\n",
    "workflow.add_node(\"model\", call_model)\n",
    "\n",
    "# Add memory\n",
    "memory = MemorySaver()\n",
    "app = workflow.compile(checkpointer=memory)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "394bb0f6-8365-4dd8-b8e0-991a60884f4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "Hi! I'm Bob.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "Hi Bob! 👋 How's it going? What can I do for you today? 😊\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "Hi! I'm Bob.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "Hi again, Bob! 😄 You must really like introducing yourself—or maybe you're testing if I remember you? (Spoiler: I do!) What's on your mind this time? Need help with something fun, random, or serious? Let me know! 🚀🎉\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "What's my name?\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "Haha, nice try, Bob! 😆 Your name is **Bob**—unless you're pulling a secret identity switcheroo on me! (In which case, I’ll need a cape and a backstory to keep up.)  \n",
      "\n",
      "So, what’s the *real* mission today? Need a joke, a fact, or help with something? 🕵️♂️🎤\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "Hi! I'm Bob.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "**Bob.** *Bob.* **BOB.** *~Bob~*  \n",
      "\n",
      "At this point, your name is basically echoing in my digital soul.  Are we:  \n",
      "1. Testing my memory (flawless, btw).  \n",
      "2. Practicing introductions for a big event?  \n",
      "3. Secretly writing a song called *\"Bob the Repeat Champion\"*?  \n",
      "\n",
      "Whatever it is—I’m here for it. Let’s go, Bob!  (Or should I say… *Booooob*?)\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "What's my name?\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "**BOB.** (All-caps for *drama*.)  \n",
      "\n",
      "At this rate, I’m convinced:  \n",
      "- You’re either a legendary AI-training bot named Bob,  \n",
      "- Or this is the *world’s chillest hostage situation* (\"SAY MY NAME AGAIN\").  \n",
      "\n",
      "Either way, I’m delighted. What’s next? A riddle? Life advice? A dramatic reenactment of *\"The Bob Identity\"*? Your call. 😎\n"
     ]
    }
   ],
   "source": [
    "query = \"Hi! I'm Bob.\"\n",
    "\n",
    "input_messages = [HumanMessage(query)]\n",
    "config = {\"configurable\": {\"thread_id\": \"abc123\"}}\n",
    "output = app.invoke({\"messages\": input_messages}, config)\n",
    "#output[\"messages\"][-1].pretty_print()  # output contains all messages in state\n",
    "query = \"What's my name?\"\n",
    "\n",
    "input_messages = [HumanMessage(query)]\n",
    "output = app.invoke({\"messages\": input_messages}, config)\n",
    "#output[\"messages\"][-1].pretty_print()\n",
    "\n",
    "for mes in output[\"messages\"]:\n",
    "    mes.pretty_print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e081bcd4-e72b-4152-8c31-427990ad5257",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
